<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width = device-width,initial-scale=1.0">
    <title></title>
</head>
<body background="bule">
使用H5创建网页
<h1>H5测试</h1>
<button onclick="sendMessage()">发送消息到原端</button>
<h2 id="myH2" style="color: red;font-size: 40px;"></h2>
<script>
    //接口的端口是用来通信的 缓存到全局
    var currentPort  //全局端口
    window.addEventListener("message",(event)=>{
		if(event.data === "trans_port"){
		  currentPort = event.ports[0]
		  if(currentPort){
		    //alert("接收端口成功")
			currentPort.onmessage = (event)=>{
				document.querySelector("#myH2").innerText = event.data
			}
		  }
		}
    })

    function sendMessage(){
		if(currentPort){
		  currentPort.postMessage(JSON.stringify({type:'change',message:Date.now()}))
		}
    }
</script>
</body>
</html>